import { createRouter, createWebHistory } from 'vue-router'
import { ElMessage } from 'element-plus'
<<<<<<< HEAD
import couponRoutes from './modules/coupon'
import dashboardRoutes from './modules/dashboard'
import messageRoutes from './modules/message'
import laundryRoutes from './modules/laundry'
=======
import dashboardRoutes from './modules/dashboard'
>>>>>>> c61637f (所有前后端正常登录)

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
<<<<<<< HEAD
    {
      path: '/login',
      name: 'Login',
      component: () => import('@/views/Login.vue'),
      meta: {
        title: '登录',
=======

    {
      path: '/login',
      name: 'Login',
      component: () => import('@/views/login/index.vue'),
      meta: {
        title: '商家登录',
>>>>>>> c61637f (所有前后端正常登录)
        requiresAuth: false
      }
    },
    {
      path: '/register',
      name: 'Register',
      component: () => import('@/views/register/index.vue'),
      meta: {
<<<<<<< HEAD
        title: '注册',
=======
        title: '商家注册',
>>>>>>> c61637f (所有前后端正常登录)
        requiresAuth: false
      }
    },
    {
      path: '/main',
      component: () => import('@/layout/index.vue'),
      redirect: '/main/dashboard',
      meta: {
        requiresAuth: true
      },
      children: [
        dashboardRoutes,
<<<<<<< HEAD
        laundryRoutes,
        {
          path: 'goods',
          name: 'Goods',
          component: () => import('@/views/goods/index.vue'),
          meta: {
            title: '商品管理',
            icon: 'Goods'
          }
        },
        {
          path: 'category',
          name: 'Category',
          component: () => import('@/views/goods/category.vue'),
          meta: {
            title: '商品分类',
            icon: 'Menu'
          }
        },
        {
          path: 'spec-template',
          name: 'SpecTemplate',
          component: () => import('@/views/goods/spec-template.vue'),
          meta: {
            title: '规格模板',
            icon: 'Grid'
          }
        },
        {
          path: 'statistics',
          name: 'Statistics',
          component: () => import('@/views/goods/statistics.vue'),
          meta: {
            title: '商品统计',
            icon: 'TrendCharts'
          }
        },
        {
          path: 'account',
          name: 'Account',
          component: () => import('@/views/account/index.vue'),
          redirect: '/main/account/index',
          meta: {
            title: '账户中心',
            icon: 'User'
          },
          children: [
            {
              path: 'index',
              name: 'AccountInfo',
              component: () => import('@/views/account/index.vue'),
              meta: {
                title: '账户信息'
              }
            },
            {
              path: 'certification',
              name: 'Certification',
              component: () => import('@/views/account/certification.vue'),
              meta: {
                title: '认证信息'
              }
            }
          ]
        },
        couponRoutes,
        messageRoutes
=======
        // 订单管理
        {
          path: 'orders',
          name: 'Orders',
          component: () => import('@/views/orders/index.vue'),
          meta: {
            title: '订单管理',
            icon: 'List'
          }
        },
        {
          path: 'orders/detail/:id',
          name: 'OrderDetail',
          component: () => import('@/views/orders/detail.vue'),
          meta: {
            title: '订单详情',
            hidden: true
          }
        },
        // 财务管理
        {
          path: 'finance',
          name: 'Finance',
          component: () => import('@/views/finance/index.vue'),
          meta: {
            title: '财务管理',
            icon: 'Wallet'
          }
        },
        // 商家设置
        // 财务管理
        {
          path: 'finance',
          name: 'Finance',
          component: () => import('@/views/finance/index.vue'),
          meta: {
            title: '财务管理',
            icon: 'Money'
          }
        },
        // 提现管理
        {
          path: 'withdrawal',
          name: 'Withdrawal',
          component: () => import('@/views/finance/withdrawal.vue'),
          meta: {
            title: '提现管理',
            icon: 'Wallet'
          }
        },
        {
          path: 'settings',
          name: 'Settings',
          component: () => import('@/views/settings/index.vue'),
          meta: {
            title: '商家设置',
            icon: 'Setting'
          }
        },
        {
          path: 'settings/bankcard',
          name: 'BankCardManagement',
          component: () => import('@/views/settings/bankcard.vue'),
          meta: {
            title: '银行卡管理',
            icon: 'CreditCard',
            hidden: true
          }
        },
        // 服务区域管理
        {
          path: 'service-areas',
          name: 'ServiceAreas',
          component: () => import('@/views/settings/service-areas.vue'),
          meta: {
            title: '服务区域管理',
            icon: 'Location'
          }
        },
        // 评价管理
        {
          path: 'reviews',
          name: 'Reviews',
          component: () => import('@/views/reviews/simple.vue'),
          meta: {
            title: '评价管理',
            icon: 'ChatDotRound'
          }
        },
        // 服务管理
        {
          path: 'services',
          name: 'Services',
          component: () => import('@/views/services/simple.vue'),
          meta: {
            title: '服务管理',
            icon: 'Star'
          }
        },
        // 营业管理
        {
          path: 'business',
          name: 'Business',
          component: () => import('@/views/business/index.vue'),
          meta: {
            title: '营业管理',
            icon: 'Shop'
          }
        },
        // 账户充值
        {
          path: 'recharge',
          name: 'Recharge',
          component: () => import('@/views/recharge/index.vue'),
          meta: {
            title: '账户充值',
            hidden: true
          }
        },

        // 保证金管理
        {
          path: 'deposit',
          name: 'Deposit',
          component: () => import('@/views/deposit/index.vue'),
          meta: {
            title: '保证金管理',
            icon: 'Wallet'
          }
        },
        // 数据统计
        {
          path: 'statistics',
          name: 'Statistics',
          component: () => import('@/views/statistics/index.vue'),
          meta: {
            title: '数据统计',
            icon: 'TrendCharts'
          }
        },
        // 消息中心
        {
          path: 'messages',
          name: 'Messages',
          component: () => import('@/views/messages/index.vue'),
          meta: {
            title: '消息中心',
            icon: 'Message'
          }
        },
        // 优惠券管理
        {
          path: 'coupons',
          name: 'Coupons',
          component: () => import('@/views/coupons/index.vue'),
          meta: {
            title: '优惠券管理',
            icon: 'Ticket'
          }
        },
        // 账户中心
        {
          path: 'account',
          name: 'Account',
          component: () => import('@/views/account/simple.vue'),
          meta: {
            title: '账户中心',
            icon: 'User'
          }
        },
        // 帮助中心
        {
          path: 'help',
          name: 'Help',
          component: () => import('@/views/help/index.vue'),
          meta: {
            title: '帮助中心',
            icon: 'QuestionFilled'
          }
        },
        // 测试页面
        {
          path: 'test/service-area',
          name: 'ServiceAreaTest',
          component: () => import('@/views/test/ServiceAreaTest.vue'),
          meta: {
            title: '服务区域选择测试',
            hidden: true
          }
        },

>>>>>>> c61637f (所有前后端正常登录)
      ]
    },
    {
      path: '/:pathMatch(.*)*',
      redirect: '/login'
    }
  ]
})

// 路由守卫
<<<<<<< HEAD
router.beforeEach((to, from, next) => {
  console.log('路由跳转:', { from: from.path, to: to.path })
  
  // 设置页面标题
  document.title = to.meta.title ? `${to.meta.title} - 商家后台管理系统` : '商家后台管理系统'
  
  // 获取token
  const token = localStorage.getItem('token')
  const userType = localStorage.getItem('userType')
  console.log('认证信息:', { token: !!token, userType })
  
  // 需要登录的页面
  if (to.meta.requiresAuth) {
    console.log('需要认证的页面')
    if (!token || userType !== 'merchant') {
      console.log('未登录或非商家用户，重定向到登录页')
=======
router.beforeEach(async (to, from, next) => {
  console.log('路由跳转:', { from: from.path, to: to.path })

  // 设置页面标题
  document.title = to.meta.title ? `${to.meta.title} - 洗护商家管理系统` : '洗护商家管理系统'

  // 获取认证信息
  const token = localStorage.getItem('merchant_token')
  const userInfo = localStorage.getItem('merchant_info')
  const merchantStatus = localStorage.getItem('merchant_status') // pending, approved, rejected

  console.log('认证信息:', {
    hasToken: !!token,
    hasUserInfo: !!userInfo,
    merchantStatus
  })

  // 公开页面，无需认证
  const publicPages = ['/login', '/register']
  if (publicPages.includes(to.path)) {
    // 如果已登录且商家状态正常，访问登录页时重定向到主页
    if (token && userInfo && merchantStatus === 'approved' && to.path === '/login') {
      console.log('已登录商家访问登录页，重定向到主页')
      next('/main/dashboard')
      return
    }
    next()
    return
  }

  // 需要认证的页面
  if (to.meta.requiresAuth !== false) {
    // 检查是否有token
    if (!token) {
      console.log('未登录，重定向到登录页')
>>>>>>> c61637f (所有前后端正常登录)
      ElMessage.warning('请先登录')
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
      return
    }
<<<<<<< HEAD
  }
  
  // 已登录用户访问登录页，重定向到首页
  if (to.path === '/login' && token) {
    console.log('已登录用户访问登录页，重定向到首页')
    next('/main/dashboard')
    return
  }
  
=======

    // 检查商家状态
    if (merchantStatus !== 'approved') {
      console.log('商家状态异常:', merchantStatus)
      if (merchantStatus === 'pending') {
        ElMessage.warning('您的商家资质正在审核中，请耐心等待')
        next('/login')
      } else if (merchantStatus === 'rejected') {
        ElMessage.error('您的商家资质审核未通过，请重新申请')
        next('/login')
      } else {
        ElMessage.error('商家状态异常，请重新登录')
        // 清除异常状态
        localStorage.removeItem('merchant_token')
        localStorage.removeItem('merchant_info')
        localStorage.removeItem('merchant_status')
        next('/login')
      }
      return
    }
  }

>>>>>>> c61637f (所有前后端正常登录)
  console.log('允许访问:', to.path)
  next()
})

export default router